package com.shenghui.enterprise.enterprise.service.impl;
import com.shenghui.enterprise.enterprise.entity.EntInfo;
import com.shenghui.enterprise.enterprise.mapper.EntInfoMapper;
import com.shenghui.enterprise.enterprise.service.IEntInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.utils.UserUtil;
import com.shenghui.enterprise.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Description: 企业信息表业务逻辑实现类
 * @Author tt
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-10-17
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Service
@Transactional
public class EntInfoServiceImpl extends ServiceImpl<EntInfoMapper, EntInfo> implements IEntInfoService {

    @Autowired
    private EntInfoMapper entInfoMapper;
    @Autowired
    private UserUtil userUtil;

    @Override
    public Result addOrUpdate(EntInfo entInfo) {
        if(GeneralUtil.isNull(entInfo.getEntId())){
            if("1".equals(entInfo.getHighTech())){
                entInfo.setHaveTime(LocalDate.parse(entInfo.getHaveTimeStr()));
            }
            entInfo.setCreateDate(LocalDateTime.now());
            entInfo.setCreateUserId(userUtil.getFrontUserId());
            entInfoMapper.insert(entInfo);
        } else {
            if("1".equals(entInfo.getHighTech())){
                entInfo.setHaveTime(LocalDate.parse(entInfo.getHaveTimeStr()));
            } else {
                entInfoMapper.updateHaveTime(entInfo.getEntId());
            }
            entInfo.setUpdateDate(LocalDateTime.now());
            entInfo.setUpdateUserId(userUtil.getFrontUserId());
            entInfoMapper.updateById(entInfo);
        }
        return Result.ok("操作成功!");
    }

    @Override
    public Result getEntInfo() {
        Map<String,Object> entInfos = entInfoMapper.getEntInfo(userUtil.getFrontUserId());
        if(entInfos != null && entInfos.size() > 0){
            return Result.ok("查询成功!",entInfos);
        }
        return Result.error("未查询到数据!");
    }
}
